package com.scau.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.scau.dao.ISaleOrderDao;
import com.scau.dao.ISerCateDao;
import com.scau.service.ISaleOrderService;
import com.scau.service.ISerCateService;
import com.scau.util.DateTransform;
import com.scau.vo.SaleOrder;
import com.scau.vo.SaleOrderItem;
import com.scau.vo.ServiceCategory;

/**
 * @author Jinglun
 * @create 2017年4月22日
 */
public class SaleOrderServiceImpl implements ISaleOrderService {

	private ISaleOrderDao saleOrderDao;
	
	@Override
	public boolean saveSaleOrder(SaleOrder saleOrder) {
		// 当前时间由Date->Timestamp
		Date date = new Date();
		DateTransform dt = new DateTransform();
		Timestamp create = dt.dToTimestamp(date);
		saleOrder.setSaleDateTime(create);
		
		return saleOrderDao.saveSaleOrder(saleOrder);
	}

	@Override
	public List<SaleOrder> listSaleOrder() {
		return saleOrderDao.listSaleOrder();
	}
	
	@Override
	public List<SaleOrder> listDebts() {
		return saleOrderDao.listDebts();
	}
	
	@Override 
	public List<SaleOrder> getOneSaleOrder(String saleOrderNo){
		return saleOrderDao.getOneSaleOrder(saleOrderNo);
	}
	
	@Override
	public boolean updateSO(String saleOrderNo){
		return saleOrderDao.updateSO(saleOrderNo);
	}

	@Override
	public List<String> eachMonthSales(String year){
		List<SaleOrder> monthSales = saleOrderDao.eachMonthSales(year);
		
		List<String> resultSales = new ArrayList<String>();
		// 先将每个月的数据设置为0
		for (int j = 0; j <= 11; j++) {
			resultSales.add("0");
		}
		// 将查询得到的销售额加到相应月份
		for (int i = 0; i < monthSales.size(); i++){
			int mon = monthSales.get(i).getSaleDateTime().getMonth(); // 月份
			int originalTotal = Integer.parseInt(resultSales.get(mon)); // 原先的总销售额
			int total = Integer.parseInt(monthSales.get(i).getTotal()) + originalTotal; // 新的总销售额
			resultSales.set(mon, total+""); // set(index,e)方法改变List的值
		}
		
		return resultSales;
	}
	
	
	public List periodSales(String start, String end){
		// 查询所有的服务类型
		List<ServiceCategory> scList = saleOrderDao.serCateList();
		System.out.println("查询的所以服务类型："+scList);
		
		String sql = "SELECT serCate.name,soi.subTotal " + 
					"From service_category AS serCate,service AS ser," +
					"sale_order AS so,sale_order_item AS soi " +
					"WHERE serCate.id = ser.serCateId " +
					"AND so.saleOrderNo = soi.saleOrderNo " +
					"AND soi.itemId = ser.id " +
					"AND so.saleDateTime BETWEEN '" + start + "' AND '" + end + "'";
		List foundList = new ArrayList();
		foundList = saleOrderDao.periodSales(sql);
		// 输出的是对象数组
		System.out.println("在service层的list：" + foundList);
		// hibernate执行sql语句查询到的是对象数组，转成json格式才能得到里面的数据
		JSONArray jsonArray = JSONArray.fromObject(foundList);
	
		// 要返回到前端的list
		List resultList = new ArrayList();
		
		for (int i = 0; i < scList.size(); i++) {
			Map<String, Object> categoryData = new HashMap<String, Object>();
			int sum = 0;
			for (int j = 0; j < jsonArray.size(); j++) {
				List list = JSONArray.fromObject(jsonArray.get(j));

				if (list.get(0).equals(scList.get(i).getName())) {
					int originalSum = Integer.parseInt((String) list.get(1));
					sum += originalSum;
				}
			}
			categoryData.put("value", sum);
			categoryData.put("name", scList.get(i).getName());
			resultList.add(categoryData);
		}
		System.out.println("结果：" + resultList);
		System.out.println(JSONArray.fromObject(resultList));
		
		return resultList;
	}
	
	// get,set methods
	public ISaleOrderDao getSaleOrderDao() {
		return saleOrderDao;
	}

	public void setSaleOrderDao(ISaleOrderDao saleOrderDao) {
		this.saleOrderDao = saleOrderDao;
	}
}
